Completed
Push — master ( 334bf8...741e42 )
by Felipe
38s
created

paginator.js ➔ ... ➔ describe(ꞌwhen under server modeꞌ)   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 214

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 214
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
  backgrid
3
  http://github.com/wyuenho/backgrid
4
5
  Copyright (c) 2013-present Cloudflare, Inc and contributors
6
  Licensed under the MIT @license.
7
*/
8
describe("A PageHandle", function () {
9
10
  var collection;
11
12
  beforeEach(function () {
13
    collection = new Backbone.PageableCollection([{
14
      id: 1
15
    }, {
16
      id: 2
17
    }, {
18
      id: 3
19
    }, {
20
      id: 4
21
    }, {
22
      id: 5
23
    }], {
24
      state: {
25
        pageSize: 2
26
      },
27
      mode: "client"
28
    });
29
  });
30
31
  describe("when under control mode", function () {
32
33
    it("can render a disabled rewind handle if current page = first page", function () {
34
      var handle = new Backgrid.Extension.PageHandle({
35
        collection: collection,
36
        isRewind: true,
37
        label: "first",
38
        title: "first"
39
      });
40
      handle.render();
41
      expect(handle.$el.hasClass("disabled")).toBe(true);
42
      expect(handle.$("a").attr("title")).toBe("first");
43
      expect(handle.$("a").text()).toBe("first");
44
    });
45
46
    it("can render a rewind handle if current page != first page", function () {
47
      collection.getPage(2);
48
      var handle = new Backgrid.Extension.PageHandle({
49
        collection: collection,
50
        isRewind: true,
51
        label: "first",
52
        title: "first"
53
      });
54
      handle.render();
55
      expect(handle.$el.hasClass("disabled")).toBe(false);
56
      expect(handle.$("a").attr("title")).toBe("first");
57
      expect(handle.$("a").text()).toBe("first");
58
    });
59
60
    it("the rewind handle will get first page on click", function () {
61
      collection.getPage(2);
62
      var handle = new Backgrid.Extension.PageHandle({
63
        collection: collection,
64
        isRewind: true,
65
        label: "first",
66
        title: "first"
67
      });
68
      handle.render();
69
      handle.$("a").click();
70
      expect(collection.state.currentPage).toBe(1);
71
    });
72
73
    it("can render a disabled back handle if current page = first page", function () {
74
      var handle = new Backgrid.Extension.PageHandle({
75
        collection: collection,
76
        isBack: true,
77
        label: "back",
78
        title: "back"
79
      });
80
      handle.render();
81
      expect(handle.$el.hasClass("disabled")).toBe(true);
82
      expect(handle.$("a").attr("title")).toBe("back");
83
      expect(handle.$("a").text()).toBe("back");
84
    });
85
86
    it("can render a back handle if current page != first page", function () {
87
      collection.getPage(2);
88
      var handle = new Backgrid.Extension.PageHandle({
89
        collection: collection,
90
        isBack: true,
91
        label: "back",
92
        title: "back"
93
      });
94
      handle.render();
95
      expect(handle.$el.hasClass("disabled")).toBe(false);
96
      expect(handle.$("a").attr("title")).toBe("back");
97
      expect(handle.$("a").text()).toBe("back");
98
    });
99
100
    it("the back handle will get the previous page on click", function () {
101
      collection.getPage(3);
102
      var handle = new Backgrid.Extension.PageHandle({
103
        collection: collection,
104
        isBack: true,
105
        label: "back",
106
        title: "back"
107
      });
108
      handle.render();
109
      handle.$("a").click();
110
      expect(collection.state.currentPage).toBe(2);
111
    });
112
113
    it("can render a disabled forward handle if current page = last page", function () {
114
      collection.getLastPage();
115
      var handle = new Backgrid.Extension.PageHandle({
116
        collection: collection,
117
        isForward: true,
118
        label: "next",
119
        title: "next"
120
      });
121
      handle.render();
122
      expect(handle.$el.hasClass("disabled")).toBe(true);
123
      expect(handle.$("a").attr("title")).toBe("next");
124
      expect(handle.$("a").text()).toBe("next");
125
    });
126
127
    it("can render a forward handle if current page != first page", function () {
128
      var handle = new Backgrid.Extension.PageHandle({
129
        collection: collection,
130
        isForward: true,
131
        label: "next",
132
        title: "next"
133
      });
134
      handle.render();
135
      expect(handle.$el.hasClass("disabled")).toBe(false);
136
      expect(handle.$("a").attr("title")).toBe("next");
137
      expect(handle.$("a").text()).toBe("next");
138
    });
139
140
    it("the forward handle will get next page on click", function () {
141
      var handle = new Backgrid.Extension.PageHandle({
142
        collection: collection,
143
        isForward: true,
144
        label: "next",
145
        title: "next"
146
      });
147
      handle.render();
148
      handle.$("a").click();
149
      expect(collection.state.currentPage).toBe(2);
150
    });
151
152
    it("can render a disabled fast forward handle if current page = last page", function () {
153
      collection.getLastPage();
154
      var handle = new Backgrid.Extension.PageHandle({
155
        collection: collection,
156
        isFastForward: true,
157
        label: "last",
158
        title: "last"
159
      });
160
      handle.render();
161
      expect(handle.$el.hasClass("disabled")).toBe(true);
162
      expect(handle.$("a").attr("title")).toBe("last");
163
      expect(handle.$("a").text()).toBe("last");
164
    });
165
166
    it("can render a fast forward handle if current page != first page", function () {
167
      var handle = new Backgrid.Extension.PageHandle({
168
        collection: collection,
169
        isFastForward: true,
170
        label: "last",
171
        title: "last"
172
      });
173
      handle.render();
174
      expect(handle.$el.hasClass("disabled")).toBe(false);
175
      expect(handle.$("a").attr("title")).toBe("last");
176
      expect(handle.$("a").text()).toBe("last");
177
    });
178
179
    it("can render a disabled forward handle if totalPages < 1", function () {
180
      var handle = new Backgrid.Extension.PageHandle({
181
        collection: new Backbone.PageableCollection(),
182
        isForward: true
183
      });
184
      handle.render();
185
      expect(handle.$el.hasClass("disabled")).toBe(true);
186
    });
187
188
    it("can render a disabled fast forward handle if totalPages < 1", function () {
189
      var handle = new Backgrid.Extension.PageHandle({
190
        collection: new Backbone.PageableCollection(),
191
        isFastForward: true
192
      });
193
      handle.render();
194
      expect(handle.$el.hasClass("disabled")).toBe(true);
195
    });
196
197
    it("the fast forward handle will get last page on click", function () {
198
      collection.getPage(2);
199
      var handle = new Backgrid.Extension.PageHandle({
200
        collection: collection,
201
        isFastForward: true,
202
        label: "last",
203
        title: "last"
204
      });
205
      handle.render();
206
      handle.$("a").click();
207
      expect(collection.state.currentPage).toBe(collection.state.lastPage);
208
    });
209
210
  });
211
212
  describe("when under discrete mode", function () {
213
214
    it("renders a 1-based label based on a 0-based pageIndex", function () {
215
      var handle = new Backgrid.Extension.PageHandle({
216
        collection: collection,
217
        pageIndex: 0
218
      });
219
      handle.render();
220
      expect(handle.$("a").text()).toBe("1");
221
    });
222
223
    it("renders a label independently of the pageIndex if one is given", function () {
224
      var handle = new Backgrid.Extension.PageHandle({
225
        collection: collection,
226
        pageIndex: 0,
227
        label: "a"
228
      });
229
      handle.render();
230
      expect(handle.$("a").text()).toBe("a");
231
    });
232
233
    it("renders a default title", function () {
234
      var handle = new Backgrid.Extension.PageHandle({
235
        collection: collection,
236
        pageIndex: 0
237
      });
238
      handle.render();
239
      expect(handle.$("a").attr("title")).toBe("Page 1");
240
    });
241
242
    it("renders the title if one is given", function () {
243
      var handle = new Backgrid.Extension.PageHandle({
244
        collection: collection,
245
        pageIndex: 0,
246
        title: "one"
247
      });
248
      handle.render();
249
      expect(handle.$("a").attr("title")).toBe("one");
250
    });
251
252
    it("renders a title template if one is given", function () {
253
      var handle = new Backgrid.Extension.PageHandle({
254
        collection: collection,
255
        pageIndex: 0,
256
        title: _.template("No. <%- label %>")
257
      });
258
      handle.render();
259
      expect(handle.$("a").attr("title")).toBe("No. 1");
260
    });
261
262
    it("renders an active page handle if current page = pageIndex", function () {
263
      var handle = new Backgrid.Extension.PageHandle({
264
        collection: collection,
265
        pageIndex: 0
266
      });
267
      handle.render();
268
      expect(handle.$el.hasClass("active")).toBe(true);
269
      expect(handle.$("a").attr("title")).toBe("Page 1");
270
      expect(handle.$("a").text()).toBe("1");
271
    });
272
273
    it("renders an page handle if current page != pageIndex", function () {
274
      var handle = new Backgrid.Extension.PageHandle({
275
        collection: collection,
276
        pageIndex: 1
277
      });
278
      handle.render();
279
      expect(handle.$el.hasClass("active")).toBe(false);
280
      expect(handle.$("a").attr("title")).toBe("Page 2");
281
      expect(handle.$("a").text()).toBe("2");
282
    });
283
284
    it("the handle will get the page on click", function () {
285
      var handle = new Backgrid.Extension.PageHandle({
286
        collection: collection,
287
        pageIndex: 1
288
      });
289
      handle.render();
290
      handle.$("a").click();
291
      expect(collection.state.currentPage).toBe(2);
292
    });
293
294
  });
295
296
});
297
298
describe("A Paginator", function () {
299
300
  var collection, paginator;
301
302
  it("can override default settings by passing options to the constructor", function () {
303
    paginator = new Backgrid.Extension.Paginator({
304
      collection: new Backbone.Collection(),
305
      windowSize: 1,
306
      slideScale: 1,
307
      renderIndexedPageHandles: false,
308
      pageHandle: Backgrid.Extension.PageHandle.extend({}),
309
      goBackFirstOnSort: false
310
    });
311
312
    expect(paginator.windowSize).not.toBe(Backgrid.Extension.Paginator.prototype.windowSize);
313
    expect(paginator.slideScale).not.toBe(Backgrid.Extension.Paginator.prototype.slideScale);
314
    expect(paginator.renderIndexedPageHandles).not.toBe(Backgrid.Extension.Paginator.prototype.renderIndexedPageHandles);
315
    expect(paginator.pageHandle).not.toBe(Backgrid.Extension.Paginator.prototype.pageHandle);
316
    expect(paginator.goBackFirstOnSort).not.toBe(Backgrid.Extension.Paginator.prototype.goBackFirstOnSort);
317
  });
318
319
  describe("when under client mode", function () {
320
321
    beforeEach(function () {
322
      collection = new Backbone.PageableCollection([{
323
        id: 1
324
      }, {
325
        id: 2
326
      }, {
327
        id: 3
328
      }, {
329
        id: 4
330
      }, {
331
        id: 5
332
      }], {
333
        state: {
334
          pageSize: 2
335
        },
336
        mode: "client"
337
      });
338
339
      paginator = new Backgrid.Extension.Paginator({
340
        collection: collection,
341
      });
342
343
      paginator.render();
344
    });
345
346
    it("#calculateWindow should return the correct start and end bound", function () {
347
      var window = paginator._calculateWindow();
348
      expect(window[0]).toBe(0);
349
      expect(window[1]).toBe(3);
350
351
      collection.fullCollection.pop();
352
      var window = paginator._calculateWindow();
353
      expect(window[0]).toBe(0);
354
      expect(window[1]).toBe(2);
355
356
      collection.fullCollection.pop();
357
      var window = paginator._calculateWindow();
358
      expect(window[0]).toBe(0);
359
      expect(window[1]).toBe(2);
360
361
      collection.fullCollection.shift();
362
      var window = paginator._calculateWindow();
363
      expect(window[0]).toBe(0);
364
      expect(window[1]).toBe(1);
365
366
      collection.fullCollection.reset();
367
      var window = paginator._calculateWindow();
368
      expect(window[0]).toBe(0);
369
      expect(window[1]).toBe(1);
370
    });
371
372
    it("renders 1 handle when the collection has <= 1 page", function () {
373
      paginator = new Backgrid.Extension.Paginator({
374
        collection: new Backbone.PageableCollection([], {
375
          state: {
376
            pageSize: 2
377
          },
378
          mode: "client"
379
        })
380
      });
381
382
      paginator.render();
383
384
      expect(paginator.$el.find("a").length).toBe(5);
385
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
386
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
387
388
      paginator = new Backgrid.Extension.Paginator({
389
        collection: new Backbone.PageableCollection([{
390
          id: 1
391
        }], {
392
          state: {
393
            pageSize: 2
394
          },
395
          mode: "client"
396
        })
397
      });
398
399
      paginator.render();
400
401
      expect(paginator.$el.find("a").length).toBe(5);
402
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
403
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
404
405
      paginator = new Backgrid.Extension.Paginator({
406
        collection: new Backbone.PageableCollection([{
407
          id: 1
408
        }, {
409
          id: 2
410
        }], {
411
          state: {
412
            pageSize: 2
413
          },
414
          mode: "client"
415
        })
416
      });
417
418
      paginator.render();
419
420
      expect(paginator.$el.find("a").length).toBe(5);
421
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
422
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
423
    });
424
425
    it("clicking on active or disabled page handles have no effect", function () {
426
      paginator = new Backgrid.Extension.Paginator({
427
        collection: new Backbone.PageableCollection([{
428
          id: 1
429
        }], {
430
          state: {
431
            pageSize: 1
432
          },
433
          mode: "client"
434
        })
435
      });
436
437
      paginator.$el.find("a").eq(0).click();
438
      expect(paginator.collection.state.currentPage).toBe(1);
439
440
      paginator.$el.find("a").eq(1).click();
441
      expect(paginator.collection.state.currentPage).toBe(1);
442
443
      paginator.$el.find("a").eq(2).click();
444
      expect(paginator.collection.state.currentPage).toBe(1);
445
446
      paginator.$el.find("a").eq(3).click();
447
      expect(paginator.collection.state.currentPage).toBe(1);
448
449
      paginator.$el.find("a").eq(4).click();
450
      expect(paginator.collection.state.currentPage).toBe(1);
451
    });
452
453
    it("has page handles that go to the correct pages when clicked", function () {
454
      // page 2
455
      paginator.$el.find("a").eq(3).click();
456
      expect(collection.state.currentPage).toBe(2);
457
458
      // page 1
459
      paginator.$el.find("a").eq(2).click();
460
      expect(collection.state.currentPage).toBe(1);
461
462
      // reset window size and rerender
463
      paginator.windowSize = 1;
464
      paginator.render();
465
466
      // last page
467
      paginator.$el.find("a").eq(4).click();
468
      expect(paginator.$el.find("a").eq(2).html()).toBe('3');
469
      expect(collection.state.currentPage).toBe(3);
470
471
      // prev page
472
      paginator.$el.find("a").eq(1).click();
473
      expect(paginator.$el.find("a").eq(2).html()).toBe('2');
474
      expect(collection.state.currentPage).toBe(2);
475
476
      // 0-based page indices
477
      collection = new Backbone.PageableCollection([{
478
        id: 1
479
      }, {
480
        id: 2
481
      }, {
482
        id: 3
483
      }], {
484
        mode: "client",
485
        state: {
486
          pageSize: 1,
487
          firstPage: 0
488
        }
489
      });
490
      paginator = new Backgrid.Extension.Paginator({
491
        collection: collection,
492
      });
493
      paginator.render();
494
495
      // next page
496
      paginator.$el.find("a").eq(3).click();
497
      expect(collection.state.currentPage).toBe(1);
498
499
      // first page
500
      paginator.$el.find("a").eq(0).click();
501
      expect(collection.state.currentPage).toBe(0);
502
    });
503
504
    it("renders page handles <= windowSize", function () {
505
      expect(paginator.$el.find("a").length).toBe(7);
506
507
      paginator.windowSize = 1;
508
      paginator.render();
509
510
      expect(paginator.$el.find("a").length).toBe(5);
511
    });
512
513
    it("refreshes upon row insertion", function () {
514
      collection.add([{
515
        id: 6
516
      }, {
517
        id: 7
518
      }]);
519
      expect(paginator.$el.find("a").length).toBe(8);
520
      expect(paginator.$el.find("a[title='Page 4']").length).toBe(1);
521
    });
522
523
    it("refreshes upon row removal", function () {
524
      collection.remove(collection.first());
525
      expect(paginator.$el.find("a").length).toBe(6);
526
      expect(paginator.$el.find("a[title='Page 3']").length).toBe(0);
527
    });
528
529
    it("refreshes upon collection reset", function () {
530
      collection.fullCollection.reset();
531
      expect(paginator.$el.find("a").length).toBe(5);
532
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
533
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
534
    });
535
536
    it("will go back to the first page on sort by default", function () {
537
      paginator.$el.find("a").eq(3).click();
538
      collection.setSorting("id", -1);
539
      collection.fullCollection.sort();
540
      collection.trigger("backgrid:sorted");
541
      expect(paginator.$el.find("li").eq(2).hasClass("active")).toBe(true);
542
543
      paginator.goBackFirstOnSort = false;
544
      paginator.$el.find("a").eq(3).click();
545
      collection.fullCollection.sort();
546
      collection.trigger("backgrid:sorted");
547
      expect(paginator.$el.find("li").eq(3).hasClass("active")).toBe(true);
548
    });
549
550
    it("displays a single page handler number 1 when the collection is empty and totalRecords is null", function () {
551
      paginator = new Backgrid.Extension.Paginator({
552
        collection: new(Backbone.PageableCollection.extend({
553
          mode: "client"
554
        }))()
555
      });
556
557
      paginator.render();
558
559
      expect(paginator.$el.find("a").length).toBe(5);
560
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
561
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
562
    });
563
564
    it("renders the overriden control page handle labels and titles", function () {
565
      paginator = new(Backgrid.Extension.Paginator.extend({
566
        controls: {
567
          back: {
568
            label: "prev",
569
            title: "prev"
570
          },
571
          forward: {
572
            label: "next",
573
            title: "next"
574
          }
575
        }
576
      }))({
577
        collection: collection
578
      });
579
      paginator.render();
580
      expect(paginator.$el.find("a").length).toBe(7);
581
      expect(paginator.$el.find("a").eq(1).html()).toBe("prev");
582
      expect(paginator.$el.find("a").eq(5).html()).toBe("next");
583
    });
584
585
    it("renders no indexed handles if renderIndexedPageHandles is false", function () {
586
      paginator = new Backgrid.Extension.Paginator({
587
        collection: collection,
588
        renderIndexedPageHandles: false
589
      });
590
      paginator.render();
591
      expect(paginator.$el.find("a").length).toBe(4);
592
      expect(paginator.$el.find("a").eq(0).attr("title")).toBe("First");
593
      expect(paginator.$el.find("a").eq(1).attr("title")).toBe("Previous");
594
      expect(paginator.$el.find("a").eq(2).attr("title")).toBe("Next");
595
      expect(paginator.$el.find("a").eq(3).attr("title")).toBe("Last");
596
    });
597
598
  });
599
600
  describe("when under server mode", function () {
601
602
    beforeEach(function () {
603
      collection = new Backbone.PageableCollection([{
604
        id: 1
605
      }], {
606
        state: {
607
          pageSize: 1,
608
          totalRecords: 3
609
        }
610
      });
611
612
      paginator = new Backgrid.Extension.Paginator({
613
        collection: collection
614
      });
615
616
      paginator.render();
617
    });
618
619
    it("clicking on active or disabled page handles have no effect", function () {
620
      paginator = new Backgrid.Extension.Paginator({
621
        collection: new Backbone.PageableCollection([{
622
          id: 1
623
        }], {
624
          state: {
625
            pageSize: 1
626
          }
627
        })
628
      });
629
630
      paginator.$el.find("a").eq(0).click();
631
      expect(paginator.collection.state.currentPage).toBe(1);
632
633
      paginator.$el.find("a").eq(1).click();
634
      expect(paginator.collection.state.currentPage).toBe(1);
635
636
      paginator.$el.find("a").eq(2).click();
637
      expect(paginator.collection.state.currentPage).toBe(1);
638
639
      paginator.$el.find("a").eq(3).click();
640
      expect(paginator.collection.state.currentPage).toBe(1);
641
642
      paginator.$el.find("a").eq(4).click();
643
      expect(paginator.collection.state.currentPage).toBe(1);
644
    });
645
646
    it("has page handles that go to the correct pages when clicked", function () {
647
      paginator.windowSize = 1;
648
      paginator.render();
649
650
      collection.url = "url";
651
652
      var oldAjax = Backbone.ajax;
653
654
      // last page
655
      Backbone.ajax = function (settings) {
656
        settings.success([{
657
          id: 3
658
        }]);
659
      };
660
      paginator.$el.find("a").eq(4).click();
661
      expect(paginator.$el.find("a").eq(2).html()).toBe('3');
662
      expect(collection.state.currentPage).toBe(3);
663
664
      // prev page
665
      Backbone.ajax = function (settings) {
666
        settings.success([{
667
          id: 2
668
        }]);
669
      };
670
      paginator.$el.find("a").eq(1).click();
671
      expect(paginator.$el.find("a").eq(2).html()).toBe('2');
672
      expect(collection.state.currentPage).toBe(2);
673
674
      // 0-based page indices
675
      collection = new Backbone.PageableCollection([{
676
        id: 1
677
      }, {
678
        id: 2
679
      }], {
680
        state: {
681
          pageSize: 2,
682
          totalRecords: 5,
683
          firstPage: 0
684
        }
685
      });
686
687
      collection.url = "url";
688
689
      paginator = new Backgrid.Extension.Paginator({
690
        collection: collection
691
      });
692
693
      paginator.render();
694
695
      // page 2
696
      Backbone.ajax = function (settings) {
697
        settings.success([{
698
          id: 3
699
        }, {
700
          id: 4
701
        }]);
702
      };
703
      paginator.$el.find("a").eq(3).click();
704
      expect(collection.state.currentPage).toBe(1);
705
706
      // page 1
707
      Backbone.ajax = function (settings) {
708
        settings.success([{
709
          id: 1
710
        }, {
711
          id: 2
712
        }]);
713
      };
714
      paginator.$el.find("a").eq(2).click();
715
      expect(collection.state.currentPage).toBe(0);
716
717
      // next page
718
      Backbone.ajax = function (settings) {
719
        settings.success([{
720
          id: 3
721
        }, {
722
          id: 4
723
        }]);
724
      };
725
      paginator.$el.find("a").eq(5).click();
726
      expect(collection.state.currentPage).toBe(1);
727
728
      // first page
729
      Backbone.ajax = function (settings) {
730
        settings.success([{
731
          id: 1
732
        }, {
733
          id: 2
734
        }]);
735
      };
736
      paginator.$el.find("a").eq(0).click();
737
      expect(collection.state.currentPage).toBe(0);
738
739
      Backbone.ajax = oldAjax;
740
    });
741
742
    it("renders page handles <= windowSize", function () {
743
      expect(paginator.$el.find("a").length).toBe(7);
744
745
      paginator.windowSize = 1;
746
      paginator.render();
747
748
      expect(paginator.$el.find("a").length).toBe(5);
749
    });
750
751
    it("displays a single page handler number 1 when the collection is empty and totalRecords is null", function () {
752
      paginator = new Backgrid.Extension.Paginator({
753
        collection: new Backbone.PageableCollection()
754
      });
755
756
      paginator.render();
757
758
      expect(paginator.$el.find("a").length).toBe(5);
759
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
760
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
761
    });
762
763
    it("refreshes upon collection reset", function () {
764
      collection.reset([{
765
        id: 1
766
      }]);
767
      expect(paginator.$el.find("a").length).toBe(7);
768
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
769
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(1);
770
      expect(paginator.$el.find("a[title='Page 3']").length).toBe(1);
771
    });
772
773
    it("renders the overriden control page handle labels and titles", function () {
774
      paginator = new(Backgrid.Extension.Paginator.extend({
775
        controls: {
776
          back: {
777
            label: "prev",
778
            title: "prev"
779
          },
780
          forward: {
781
            label: "next",
782
            title: "next"
783
          }
784
        }
785
      }))({
786
        collection: collection
787
      });
788
      paginator.render();
789
      expect(paginator.$el.find("a").length).toBe(7);
790
      expect(paginator.$el.find("a").eq(1).html()).toBe("prev");
791
      expect(paginator.$el.find("a").eq(5).html()).toBe("next");
792
    });
793
794
    it("renders no indexed handles if renderIndexedPageHandles is false", function () {
795
      paginator = new Backgrid.Extension.Paginator({
796
        collection: collection,
797
        renderIndexedPageHandles: false
798
      });
799
      paginator.render();
800
      expect(paginator.$el.find("a").length).toBe(4);
801
      expect(paginator.$el.find("a").eq(0).attr("title")).toBe("First");
802
      expect(paginator.$el.find("a").eq(1).attr("title")).toBe("Previous");
803
      expect(paginator.$el.find("a").eq(2).attr("title")).toBe("Next");
804
      expect(paginator.$el.find("a").eq(3).attr("title")).toBe("Last");
805
    });
806
807
    it("does not refetch if already on first page when sorting", function () {
808
      spyOn(collection, "getFirstPage").and.stub();
809
      collection.trigger("backgrid:sorted");
810
      expect(collection.getFirstPage.calls.count()).toEqual(0);
811
    });
812
813
  });
814
815
  describe("when under infinite mode", function () {
816
817
    beforeEach(function () {
818
      collection = new(Backbone.PageableCollection.extend({
819
        url: "http://www.example.com",
820
      }))([{
821
        id: 1
822
      }, {
823
        id: 2
824
      }, {
825
        id: 3
826
      }, {
827
        id: 4
828
      }, {
829
        id: 5
830
      }], {
831
        state: {
832
          pageSize: 2,
833
          totalRecords: 5
834
        },
835
        mode: "infinite"
836
      });
837
838
      paginator = new Backgrid.Extension.Paginator({
839
        collection: collection
840
      });
841
842
      paginator.render();
843
    });
844
845
    it("renders the overriden control page handle labels and titles", function () {
846
      paginator = new(Backgrid.Extension.Paginator.extend({
847
        controls: {
848
          back: {
849
            label: "prev",
850
            title: "prev"
851
          },
852
          forward: {
853
            label: "next",
854
            title: "next"
855
          }
856
        }
857
      }))({
858
        collection: collection
859
      });
860
      paginator.render();
861
      expect(paginator.$el.find("a").length).toBe(7);
862
      expect(paginator.$el.find("a").eq(1).html()).toBe("prev");
863
      expect(paginator.$el.find("a").eq(5).html()).toBe("next");
864
    });
865
866
    it("renders no indexed handles if renderIndexedPageHandles is false", function () {
867
      paginator = new Backgrid.Extension.Paginator({
868
        collection: collection,
869
        renderIndexedPageHandles: false
870
      });
871
      paginator.render();
872
      expect(paginator.$el.find("a").length).toBe(4);
873
      expect(paginator.$el.find("a").eq(0).attr("title")).toBe("First");
874
      expect(paginator.$el.find("a").eq(1).attr("title")).toBe("Previous");
875
      expect(paginator.$el.find("a").eq(2).attr("title")).toBe("Next");
876
      expect(paginator.$el.find("a").eq(3).attr("title")).toBe("Last");
877
    });
878
879
  });
880
881
  describe("hiding paginator when <= 1 total pages", function () {
882
    it("renders 0 handles when the collection has 1 page", function () {
883
      paginator = new Backgrid.Extension.Paginator({
884
        collection: new Backbone.PageableCollection([], {
885
          state: {
886
            pageSize: 2
887
          },
888
          mode: "client"
889
        }),
890
        renderMultiplePagesOnly: true
891
      });
892
893
      paginator.render();
894
895
      expect(paginator.$el.find("a").length).toBe(0);
896
897
      paginator = new Backgrid.Extension.Paginator({
898
        collection: new Backbone.PageableCollection([{
899
          id: 1
900
        }], {
901
          state: {
902
            pageSize: 2
903
          },
904
          mode: "client"
905
        }),
906
        renderMultiplePagesOnly: true
907
      });
908
909
      paginator.render();
910
911
      expect(paginator.$el.find("a").length).toBe(0);
912
913
      paginator = new Backgrid.Extension.Paginator({
914
        collection: new Backbone.PageableCollection([{
915
          id: 1
916
        }, {
917
          id: 2
918
        }], {
919
          state: {
920
            pageSize: 2
921
          },
922
          mode: "client"
923
        }),
924
        renderMultiplePagesOnly: true
925
      });
926
927
      paginator.render();
928
929
      expect(paginator.$el.find("a").length).toBe(0);
930
931
      paginator = new Backgrid.Extension.Paginator({
932
        collection: new Backbone.PageableCollection([{
933
          id: 1
934
        }, {
935
          id: 2
936
        }, {
937
          id: 3
938
        }], {
939
          state: {
940
            pageSize: 2
941
          },
942
          mode: "client"
943
        }),
944
        renderMultiplePagesOnly: true
945
      });
946
947
      paginator.render();
948
949
      expect(paginator.$el.find("a").length).toBe(6);
950
      expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
951
      expect(paginator.$el.find("a[title='Page 2']").length).toBe(1);
952
    });
953
954
  });
955
956
});
957